package com.shycloud.mido.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.constant.enums.LoginTypeEnum;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.annotation.Inner;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.entity.MemberOpinion;
import com.shycloud.mido.member.api.entity.SmOpinionProblemList;
import com.shycloud.mido.member.service.MobileService;
import com.shycloud.mido.member.service.MemberOauthService;
import com.shycloud.mido.member.service.MemberService;
import com.shycloud.mido.member.service.MemberServiceV2;
import com.shycloud.mido.member.service.SmOpinionProblemListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.shycloud.mido.organ.api.enums.MessageCodeEnum;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 会员体系手机接口
 *
 * @author nianhua.jiang
 * @date 2020/07/14
 * <p>
 */
@RestController
@AllArgsConstructor
@RequestMapping("/v1/mobile")
@CrossOrigin(origins = "*")
@Api(value = "mobile", description = "会员体系手机接口")
@ApiIgnore
public class MobileController {

	/** 移动端 service */
	private final MobileService mobileService;
	private final MemberOauthService memberOauthService;
	private final MemberService memberService;
	private MemberServiceV2 memberV2Service;
	/** redis */
	private final RedisTemplate redisTemplate;
	/** 问题点列表 */
	private final SmOpinionProblemListService opinionProblemListService;

	/**
	 * 通过用户名查询用户、角色信息
	 *
	 * @return 用户信息
	 */
	@Inner
	@GetMapping("/info/{username}")
	public R info(@PathVariable String username) {
		return new R<>(memberOauthService.getMemberDetailsNew(username));
	}

	/**
	 * 获取用户身份
	 *
	 * @param
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/4/2 16:08
	 */
	@GetMapping("/infoV2/{username}")
	public R infoV2(@PathVariable String username) {
		Member member = memberService.getOne(Wrappers.<Member>query()
				.lambda().eq(Member::getUsername, username).or().eq(Member::getPhone, username));
		if (member == null) {
			return new R<>(Boolean.FALSE, String.format("用户信息为空 %s", username));
		}
		return new R<>(member.getIsTeacher());
	}


	/**
	 * 根据type@code查询用户
	 *
	 * @return 用户信息
	 */
	@Inner
	@GetMapping("/load/{inStr}")
	public R load(@PathVariable String inStr) {
		return new R<>(memberOauthService.getMemberDetails(inStr));
	}

	/**
	 * 发送验证码接口
	 *
	 * @param mobile
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/11/16 19:35
	 **/
	@GetMapping("/send/{mobile}")
	public R sendSmsCode(@PathVariable String mobile) {
		return mobileService.sendSmsCode(mobile);
	}

	@GetMapping("sendRegisterCode")
	public R sendRegisterCode(String mobile) {
		return mobileService.sendSmsRegisterCode(mobile);
	}

	@GetMapping("registerByCode")
	public R registerByCode(String mobile, String code) {
		String password = "123456";
		String preStr = "register";
		Object codeObj = redisTemplate.opsForValue().get(preStr + "@" + mobile);
		if (null == codeObj) {
			return new R<>(Boolean.FALSE, MessageCodeEnum.MEMBER_ERROR_104.getMessage())
					.setCode(MessageCodeEnum.MEMBER_ERROR_104.getCode());
		}
		// 验证码错误
		else if (!code.equals(codeObj.toString())) {
			return new R<>(Boolean.FALSE, MessageCodeEnum.MEMBER_ERROR_105.getMessage())
					.setCode(MessageCodeEnum.MEMBER_ERROR_105.getCode());
		}
		return memberV2Service.register(mobile, password, null);
	}

	/**
	 * 发送注册验证码
	 *
	 * @param mobile 手机号
	 * @return R
	 */
	@PostMapping("/register/{mobile}")
	public R registerBySmsCode(@PathVariable String mobile, String username, String password, String captcha) {

		return mobileService.registerBySmsCode(mobile, username, password, captcha);
	}

	/**
	 * 修改密码
	 *
	 * @param mobile 手机号
	 * @return R
	 */
	@PostMapping("/changepassword/{mobile}")
	public R changePassword(@PathVariable String mobile, String captcha, String newpassword) {
		return mobileService.changePassword(mobile, captcha, newpassword);
	}

	/**
	 * 意见反馈
	 *
	 * @param memberOpinion
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/14 10:06
	 **/
	@PostMapping("/saveOpinion")
	public R saveOpinion(@RequestBody MemberOpinion memberOpinion) {
		return new R(mobileService.saveOpinion(memberOpinion));
	}

	@GetMapping("getProblemPointList")
	public R getProblemPointList() {
		return new R(opinionProblemListService.list(new QueryWrapper<SmOpinionProblemList>().eq(SmOpinionProblemList.COL_STATUS, CommonConstants.STATUS_NORMAL)));
	}

	/**
	 * 微信登录
	 *
	 * @param openid 微信号
	 * @return R
	 */
	@PostMapping("/wxLogin/{openid}")
	public R wxLogin(@PathVariable String openid) {
		return mobileService.wxLogin(openid);
	}

	/**
	 * 微信绑定系统账号
	 *
	 * @param openid  微信号
	 * @param mobile  手机号
	 * @param captcha 验证码
	 * @return R
	 */
	@PostMapping("/wxBind/{openid}")
	public R wxBind(@PathVariable String openid, String mobile, String captcha) {
		return mobileService.wxBind(openid, mobile, captcha);
	}

	/**
	 * 微信绑定注册账号
	 *
	 * @param openid   微信号
	 * @param mobile   手机号
	 * @param username 用户名
	 * @param password 密码
	 * @return R
	 */
	@PostMapping("/wxRegister/{openid}")
	public R wxRegister(@PathVariable String openid, String mobile, String username, String password) {
		return mobileService.wxRegister(openid, mobile, username, password);
	}

	/**
	 * 上传头像
	 *
	 * @param file 头像文件
	 * @return R
	 */
	@PostMapping("/updHeadPhoto/{type}")
	public R updHeadPhoto(@RequestParam("File") MultipartFile file, @PathVariable String type) {
		return mobileService.updHeadPhoto(file, type);
	}

	/**
	 * 获取关联学生列表
	 *
	 * @param userid 会员id
	 * @return R
	 */
	@PostMapping("/getStudentList/{userid}")
	public R getStudentList(@PathVariable String userid) {
		return mobileService.getStudentList(userid);
	}

	/**
	 * 获取用户信息
	 *
	 * @param userid 会员id
	 * @return R
	 */
	@PostMapping("/getMemberInfo/{userid}")
	public R getMemberInfo(@PathVariable String userid) {
		return mobileService.getMemberInfo(userid);
	}

	/**
	 * 获取用户实名认证信息
	 *
	 * @param userid 会员id
	 * @return R
	 */
	@PostMapping("/getMemberIdcard/{userid}")
	public R getMemberIdcard(@PathVariable String userid) {
		return mobileService.getMemberIdcard(userid);
	}

	/**
	 * 绑定会员
	 *
	 * @param parentId  家长id
	 * @param studentId 学生id
	 * @return R
	 */
	@PostMapping("/bindMember")
	public R bindMember(String parentId, String studentId) {
		return mobileService.bindMember(parentId, studentId);
	}

	/**
	 * 解绑会员
	 *
	 * @param parentId  家长id
	 * @param studentId 学生id
	 * @return R
	 */
	@PostMapping("/unbindMember")
	public R unbindMember(String parentId, String studentId) {
		return mobileService.unbindMember(parentId, studentId);
	}

	/**
	 * 获取会员考级信息
	 *
	 * @param userid 会员id
	 * @return R
	 */
	@PostMapping("/getStudentInfo/{userid}")
	public R getStudentInfo(@PathVariable String userid) {
		return mobileService.getStudentInfo(userid);
	}

	/**
	 * 获取教师信息
	 *
	 * @param userid 会员id
	 * @return R
	 */
	@PostMapping("/getTeacherInfo/{userid}")
	public R getTeacherInfo(@PathVariable String userid) {
		return mobileService.getTeacherInfo(userid);
	}

	/**
	 * 忘记密码
	 *
	 * @param map
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/29 13:29
	 */
	@PostMapping("forgetPwd")
	@ApiOperation("忘记密码")
	public R forgetPwd(@RequestBody Map<String, String> map) {

		//电话号
		String phone = map.get("phone");
		//验证码
		String code = map.get("code");
		//密码
		String pwd = map.get("pwd");

		//获取redis存储的验证码
		String redisCode = (String) redisTemplate.opsForValue()
				.get(CommonConstants.DEFAULT_CODE_KEY + LoginTypeEnum.SMS.getType() + "@" + phone);

		if (redisCode == null) {
			return new R(CommonConstants.FAIL, "验证码已过期", false);
		}

		//校验验证码
		if (!redisCode.equals(code)) {
			return new R(CommonConstants.FAIL, "验证码不正确", false);
		}

		//修改密码
		int result = mobileService.forgetPwd(phone, pwd);
		if (result == 1) {
			//删除验证码redis
			redisTemplate.delete(CommonConstants.DEFAULT_CODE_KEY + LoginTypeEnum.SMS.getType() + "@" + phone);
			//返回结果
			return new R(CommonConstants.SUCCESS, "密码更新成功！", true);
		} else if (result == 0) {
			return new R(CommonConstants.FAIL, "密码更新失败！", false);
		} else {
			return new R(CommonConstants.FAIL, "用户信息获取失败，请联系客服处理问题！", false);
		}
	}

	/**
	 * 绑定手机
	 *
	 * @param phone
	 * @param code
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/9/4 16:46
	 **/
	@GetMapping("bindPhone")
	public R bindPhone(String phone, String code, String openId) {
		return new R(mobileService.bindPhone(phone, code, openId));
	}

	/**
	 * 绑定手机
	 *
	 * @param phone
	 * @param code
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/10/16 16:02
	 **/
	@GetMapping("iosBindPhone")
	public R iosBindPhone(String phone, String code, String userId) {
		return new R(mobileService.iosBindPhone(phone, code, userId));
	}

}
